package top.kaoshanji.ch05;

import com.google.common.io.Resources;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单 ch05-04：Class资源(5.7.3节)
 * author kaoshanji
 * ^_^
 * create time 2024/11/26 17:13
 */
public class ResourceTest {

    private static final Logger logger = LogManager.getLogger(ResourceTest.class);

    public static void main(String[] args) throws IOException {
        logger.info("...代码清单 ch05-04：Class资源(5.7.3节)...");

  //      Class cl = ResourceTest.class;

        // 靠，这个路径写不对，先跳过....
//        URL aboutURL = cl.getResource("about.gif");
//        var icon = new ImageIcon(aboutURL);
//
//        InputStream stream = cl.getResourceAsStream("ch05/data/about.txt");
//        var about = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
//
//        InputStream stream2 = cl.getResourceAsStream("ch05/corejava/title.txt");
//        var title = new String(stream2.readAllBytes(), StandardCharsets.UTF_8).trim();


        // 尴尬了..这里演示的是 Class 访问资源的方法，结果使用第三方库解决了...

        URL aboutURL = Resources.getResource("ch05/about.gif");
        var icon = new ImageIcon(aboutURL);

        String about = Resources.toString(Resources.getResource("ch05/data/about.txt"), StandardCharsets.UTF_8);

        String title = Resources.toString(Resources.getResource("ch05/corejava/title.txt"), StandardCharsets.UTF_8);

        JOptionPane.showMessageDialog(null, about, title, JOptionPane.INFORMATION_MESSAGE, icon);

    }

}
